library(foreign)
library(car)
library(dplyr)

mil_conf.df <- read.csv("~/Dropbox/Public_Conf_Mil/Data_and_code/mil_conf.csv")

## Remove missing row
mil_conf.df <- subset(mil_conf.df, is.na(mil_conf.df$weight) == FALSE)

#### STRONGLY AGREE-DISAGREE ####

ad_varlist <- c("Q12A", "Q12B", "Q12C", "Q12D", "Q12E", "Q12F", "Q12G", "Q12H", "Q12I", "Q12J",
                "Q15A", "Q15B", "Q15C", "Q15D", "Q15E", "Q15F", "Q15G", "Q19A", "Q19B", "Q19C",
                "Q19D", "Q19E", "Q19F", "Q19G", "Q19H", "Q19AA", "Q19AB", "Q19AC", "Q19AD", 
                "Q19AE", "Q19AF", "Q19AG", "Q19AH", "Q27A", "Q27B", "Q27C", "Q27D", "Q27E",
                "Q27F", "Q27G", "Q27H", "Q27I", "Q27J", "Q33A", "Q33B", "Q33C", "Q33D", "Q33E",
                "Q33F", "Q33G", "Q33H", "Q33AA", "Q33AB", "Q33AC", "Q33AD", "Q33AE", "Q33AF",
                "Q33AG", "Q33AH", "Q42A", "Q42B", "Q42C", "Q42D", "Q42E", "Q42F", "Q42G")

for(i in ad_varlist) {
  mil_conf.df[[paste0(i, "_r")]] <- mil_conf.df[[i]]
  mil_conf.df[[paste0(i, "_r")]] <- recode(mil_conf.df[[paste0(i, "_r")]], 
                                           "1=1;2=0.75;3=0.5;4=0.25;5=0")
}
# set of "_r" recoded variables keep DK/skipped/refused as previously coded

for(i in ad_varlist) {
  mil_conf.df[[paste0(i, "_r1")]] <- mil_conf.df[[i]]
  mil_conf.df[[paste0(i, "_r1")]] <- car::recode(mil_conf.df[[paste0(i, "_r1")]], 
                                                 "1=1;2=0.75;3=0.5;4=0.25;5=0")
  mil_conf.df[[paste0(i, "_r1")]][mil_conf.df[[i]] == 77 | mil_conf.df[[i]] == 98 |
                                    mil_conf.df[[i]] == 99 | mil_conf.df[[i]] == 6] <- NA
}
# set of "_r1" recoded variables replace DK/skipped/refused as NA

for(i in ad_varlist) {
  mil_conf.df[[paste0(i, "_r2")]] <- mil_conf.df[[i]]
  mil_conf.df[[paste0(i, "_r2")]] <- recode(mil_conf.df[[paste0(i, "_r2")]], 
                                            "1=1;2=0.75;3=0.5;4=0.25;5=0;6=0.5;77=0.5;
                                            98=0.5;99=0.5")
}
# set of "_r2" recoded variables place DK/skipped/refused at 0.5 (neither agree/disagree)

for(i in ad_varlist) {
  mil_conf.df[[paste0(i, "_d")]] <- mil_conf.df[[i]]
  mil_conf.df[[paste0(i, "_d")]] <- car::recode(mil_conf.df[[paste0(i, "_r1")]], 
                                       "1=1;0.75=1;0.5=0;0.25=0;5=0")
  mil_conf.df[[paste0(i, "_d")]][mil_conf.df[[i]] == 77 | mil_conf.df[[i]] == 98 |
                                   mil_conf.df[[i]] == 99 | mil_conf.df[[i]] == 6] <- NA
}
# recoded variables set skipped/missing as NA, recode as dummy for agreement

#### KNOWLEDGE QUESTIONS ####

## Binary variables for correct answers

mil_conf.df$Q1A_cor <- NA # 3 - Merchant Marines
mil_conf.df$Q1A_cor <- ifelse(mil_conf.df$Q1A == 3, 1, 0)
mil_conf.df$Q1A_cor[is.na(mil_conf.df$Q1A_cor)] <- 0

mil_conf.df$Q1B_cor <- NA # 4 - Army
mil_conf.df$Q1B_cor <- ifelse(mil_conf.df$Q1B == 4, 1, 0)
mil_conf.df$Q1B_cor[is.na(mil_conf.df$Q1B_cor)] <- 0

mil_conf.df$Q2A_cor <- NA # 3 - Vietnam, Iraq, Afghanistan, Gulf War
mil_conf.df$Q2A_cor <- ifelse(mil_conf.df$Q2A == 3, 1, 0)
mil_conf.df$Q2A_cor[is.na(mil_conf.df$Q2A_cor)] <- 0

mil_conf.df$Q2B_1_cor <- NA # 1 - Yes
mil_conf.df$Q2B_1_cor <- ifelse(mil_conf.df$Q2B_1 == 1, 1, 0)
mil_conf.df$Q2B_1_cor[is.na(mil_conf.df$Q2B_1_cor)] <- 0
mil_conf.df$Q2B_1_cor2 <- NA
mil_conf.df$Q2B_1_cor2 <- ifelse(mil_conf.df$Q2B_1 == 1, 0.2, 0)
mil_conf.df$Q2B_1_cor2[is.na(mil_conf.df$Q2B_1_cor2)] <- 0

mil_conf.df$Q2B_2_cor <- NA # 1 - Yes
mil_conf.df$Q2B_2_cor <- ifelse(mil_conf.df$Q2B_2 == 1, 1, 0)
mil_conf.df$Q2B_2_cor[is.na(mil_conf.df$Q2B_2_cor)] <- 0
mil_conf.df$Q2B_2_cor2 <- NA
mil_conf.df$Q2B_2_cor2 <- ifelse(mil_conf.df$Q2B_2 == 1, 0.2, 0)
mil_conf.df$Q2B_2_cor2[is.na(mil_conf.df$Q2B_2_cor2)] <- 0

mil_conf.df$Q2B_3_cor <- NA # 1 - Yes
mil_conf.df$Q2B_3_cor <- ifelse(mil_conf.df$Q2B_3 == 1, 1, 0)
mil_conf.df$Q2B_3_cor[is.na(mil_conf.df$Q2B_3_cor)] <- 0
mil_conf.df$Q2B_3_cor2 <- NA
mil_conf.df$Q2B_3_cor2 <- ifelse(mil_conf.df$Q2B_3 == 1, 0.2, 0)
mil_conf.df$Q2B_3_cor2[is.na(mil_conf.df$Q2B_3_cor2)] <- 0

mil_conf.df$Q2B_4_cor <- NA # 1 - Yes
mil_conf.df$Q2B_4_cor <- ifelse(mil_conf.df$Q2B_4 == 1, 1, 0)
mil_conf.df$Q2B_4_cor[is.na(mil_conf.df$Q2B_4_cor)] <- 0
mil_conf.df$Q2B_4_cor2 <- NA
mil_conf.df$Q2B_4_cor2 <- ifelse(mil_conf.df$Q2B_4 == 1, 0.2, 0)
mil_conf.df$Q2B_4_cor2[is.na(mil_conf.df$Q2B_4_cor2)] <- 0

mil_conf.df$Q2B_5_cor <- NA # 1 - Yes
mil_conf.df$Q2B_5_cor <- ifelse(mil_conf.df$Q2B_5 == 1, 1, 0)
mil_conf.df$Q2B_5_cor[is.na(mil_conf.df$Q2B_5_cor)] <- 0
mil_conf.df$Q2B_5_cor2 <- NA
mil_conf.df$Q2B_5_cor2 <- ifelse(mil_conf.df$Q2B_5 == 1, 0.2, 0)
mil_conf.df$Q2B_5_cor2[is.na(mil_conf.df$Q2B_5_cor2)] <- 0

mil_conf.df$Q2B_cor <- NA
mil_conf.df$Q2B_cor <- ifelse(mil_conf.df$Q2B_1_cor == 1 & mil_conf.df$Q2B_2_cor == 1 &
                                mil_conf.df$Q2B_3_cor == 1 & mil_conf.df$Q2B_4_cor == 1 &
                                mil_conf.df$Q2B_5_cor == 1, 1, 0)

mil_conf.df$Q3_cor <- NA # 3 - President
mil_conf.df$Q3_cor <- ifelse(mil_conf.df$Q3 == 3, 1, 0)

mil_conf.df$Q4_cor <- NA # 1 - Yes
mil_conf.df$Q4_cor <- ifelse(mil_conf.df$Q4 == 1, 1, 0)

mil_conf.df$Q5_cor <- NA # 2 - Meal, Ready to Eat
mil_conf.df$Q5_cor <- ifelse(mil_conf.df$Q5 == 2, 1, 0)

mil_conf.df$Q6_cor <- NA # 3 - after the attack on Pearl Harbor in December 1941
mil_conf.df$Q6_cor <- ifelse(mil_conf.df$Q6 == 3, 1, 0)

## Knowledge scales

mil_conf.df$Knowledge <- NA
mil_conf.df$Knowledge <- mil_conf.df$Q1A_cor + mil_conf.df$Q1B_cor + mil_conf.df$Q2A_cor +
  mil_conf.df$Q2B_cor + mil_conf.df$Q3_cor + mil_conf.df$Q4_cor + mil_conf.df$Q5_cor + 
  mil_conf.df$Q6_cor
# Knowledge: additive knowledge scale, Q2B treated as all or nothing

mil_conf.df$Knowledge1 <- NA
mil_conf.df$Knowledge1 <- mil_conf.df$Q1A_cor + mil_conf.df$Q1B_cor + mil_conf.df$Q2A_cor +
  mil_conf.df$Q2B_1_cor2 + mil_conf.df$Q2B_2_cor2 + mil_conf.df$Q2B_3_cor2 + mil_conf.df$Q2B_4_cor2 +
  mil_conf.df$Q2B_5_cor2 + mil_conf.df$Q3_cor + mil_conf.df$Q4_cor + mil_conf.df$Q5_cor +
  mil_conf.df$Q6_cor
# Knowledge1: additive knowledge scale, Q2B treated as 0.2 per correct answer

#### CONFIDENCE SCALES ####

## Q9 - confidence in the military ##

mil_conf.df$Q9_r <- mil_conf.df$Q9 - 1
mil_conf.df$Q9_r <- recode(mil_conf.df$Q9_r, "97=98")
# Q9_r: keeps 98 (skipped) as previously coded

mil_conf.df$Q9_r1 <- mil_conf.df$Q9 - 1
mean(mil_conf.df$Q9_r[mil_conf.df$Q9_r <= 10], na.rm = TRUE)
mil_conf.df$Q9_r1[mil_conf.df$Q9_r == 98] <- 7
# Q9_r1: recodes 98 (skipped) as the rounded mean (7.412, set at 7)

mil_conf.df$Q9_r2 <- mil_conf.df$Q9 - 1
median(mil_conf.df$Q9_r, na.rm = TRUE)
mil_conf.df$Q9_r2[mil_conf.df$Q9_r == 98] <- 8
# Q9_r2: recodes 98 (skipped) as the median (8)

## Q10A - confidence in active generals/admirals ##

mil_conf.df$Q10A_r <- mil_conf.df$Q10A - 1
mil_conf.df$Q10A_r <- recode(mil_conf.df$Q10A_r, "97=98")
# Q10A_r: keeps 98 (skipped) as previously coded

mil_conf.df$Q10A_r1 <- mil_conf.df$Q10A - 1
mean(mil_conf.df$Q10A_r[mil_conf.df$Q10A_r <= 10], na.rm = TRUE)
mil_conf.df$Q10A_r1[mil_conf.df$Q10A_r == 98] <- 7
# Q10A_r1: recodes 98 (skipped) as the rounded mean (7.021, set at 7)

mil_conf.df$Q10A_r2 <- mil_conf.df$Q10A - 1
median(mil_conf.df$Q10A_r, na.rm = TRUE)
mil_conf.df$Q10A_r2[mil_conf.df$Q10A_r == 98] <- 8
# Q10A_r2: recodes 98 (skipped) as the median (8)

## Q11A - confidence in retired generals/admirals ##

mil_conf.df$Q11A_r <- mil_conf.df$Q11A
mil_conf.df$Q11A_r <- recode(mil_conf.df$Q11A_r, "97=98")
# Q11A_r: keeps 98 (skipped) as previously coded

mil_conf.df$Q11A_r1 <- mil_conf.df$Q11A
mean(mil_conf.df$Q11A_r[mil_conf.df$Q11A_r <= 10], na.rm = TRUE)
mil_conf.df$Q11A_r1[mil_conf.df$Q11A_r == 98] <- 7
# Q11A_r1: recodes 98 (skipped) as the rounded mean (7.025, set at 7)

mil_conf.df$Q11A_r2 <- mil_conf.df$Q11A
median(mil_conf.df$Q11A_r, na.rm = TRUE)
mil_conf.df$Q11A_r2[mil_conf.df$Q11A_r == 98] <- 8
# Q11A_r2: recodes 98 (skipped) as the median (8)

#### CREDIBILITY SCALES ####

cr_varlist <- c("Q12A", "Q12B", "Q12C", "Q12D", "Q12E", "Q12F", "Q12G", "Q15A", "Q15B", "Q15C",
                "Q15D", "Q15E", "Q15F", "Q15G")

for(i in cr_varlist) {
  mil_conf.df[[paste0(i, "_c")]] <- mil_conf.df[[i]]
  mil_conf.df[[paste0(i, "_c")]] <- recode(mil_conf.df[[paste0(i, "_c")]], 
                                           "77=3;98=3;99=3")
}
# set of "_c" variables recodes DK/skipped/refused as 3 (neither agree/disagree)

for(i in cr_varlist) {
  mil_conf.df[[paste0(i, "_c1")]] <- 6 - mil_conf.df[[i]]
  mil_conf.df[[paste0(i, "_c1")]] <- recode(mil_conf.df[[paste0(i, "_c1")]], 
                                            "-71=3;-92=3;-93=3")
}
# set of "_c1" variables recodes DK/skipped/refused as 3 (neither agree/disagree) and reverses
  # coding - increasing number means increasing agreement

for(i in cr_varlist) {
  mil_conf.df[[paste0(i, "_c2")]] <- mil_conf.df[[i]]
  mil_conf.df[[paste0(i, "_c2")]][mil_conf.df[[i]] == 77 | mil_conf.df[[i]] == 98 |
                                    mil_conf.df[[i]] == 99] <- NA
}
# set of "_c2" variables recodes DK/skipped/refused as NA

for(i in cr_varlist) {
  mil_conf.df[[paste0(i, "_c3")]] <- 6 - mil_conf.df[[i]]
  mil_conf.df[[paste0(i, "_c3")]][mil_conf.df[[i]] == 77 | mil_conf.df[[i]] == 98 |
                                    mil_conf.df[[i]] == 99] <- NA
}
# set of "_c3" variables recodes DK/skipped/refused as NA and reverses coding

## Mil_Credibility - Q12A, Q12C-G ##

mil_conf.df$Mil_Credibility <- mil_conf.df$Q12A_c + mil_conf.df$Q12C_c1 + mil_conf.df$Q12D_c1 +
  mil_conf.df$Q12E_c1 + mil_conf.df$Q12F_c1 + mil_conf.df$Q12G_c1
# Mil_Credibility: additive scale, increasing means increasing credibility, DK coded as 3
  # note Q12A reverse coded for all Q12 scales

mil_conf.df$Mil_Credibility_na <- mil_conf.df$Q12A_c2 + mil_conf.df$Q12C_c3 + mil_conf.df$Q12D_c3 +
  mil_conf.df$Q12E_c3 + mil_conf.df$Q12F_c3 + mil_conf.df$Q12G_c3
# Mil_Credibility_na: additive scale, increasing means increasing credibility, DK coded as NA

## Mil_Credibility2 - Q12A-G ##

mil_conf.df$Mil_Credibility2 <- mil_conf.df$Q12A_c + mil_conf.df$Q12B_c1 + mil_conf.df$Q12C_c1 + 
  mil_conf.df$Q12D_c1 + mil_conf.df$Q12E_c1 + mil_conf.df$Q12F_c1 + mil_conf.df$Q12G_c1
# Mil_Credibility2: additive scale, increasing means increasing credibility, DK coded as 3
  # note Q12A reverse coded for all Q12 scales

mil_conf.df$Mil_Credibility2_na <- mil_conf.df$Q12A_c2 + mil_conf.df$Q12B_c3 +
  mil_conf.df$Q12C_c3 + mil_conf.df$Q12D_c3 + mil_conf.df$Q12E_c3 + mil_conf.df$Q12F_c3 + 
  mil_conf.df$Q12G_c3
# Mil_Credibilit2y_na: additive scale, increasing means increasing credibility, DK coded as NA

## Inst_Credibility - Q15A-G ##

mil_conf.df$Inst_Credibility <- mil_conf.df$Q15A_c + mil_conf.df$Q15B_c1 + mil_conf.df$Q15C_c1 +
  mil_conf.df$Q15D_c1 + mil_conf.df$Q15E_c1 + mil_conf.df$Q15F_c1 + mil_conf.df$Q15G_c1
# Inst_Credibility: additive scale, increasing means increasing credibility, DK coded as 3
  # note Q15A reverse coded for all Q15 scales

mil_conf.df$Inst_Credibility_na <- mil_conf.df$Q15A_c2 + mil_conf.df$Q15B_c3 + mil_conf.df$Q15C_c3 +
  mil_conf.df$Q15D_c3 + mil_conf.df$Q15E_c3 + mil_conf.df$Q15F_c3 + mil_conf.df$Q15G_c3
# Inst_Credibility_na: additive scale, increasing means increasing credibility, DK coded as NA

#### VETERAN PERIOD OF SERVICE ####

mil_conf.df$VET_911 <- NA
mil_conf.df$VET_911[mil_conf.df$VET_DATE1 < 2001] <- 0 # before 9/11
mil_conf.df$VET_911[mil_conf.df$VET_DATE <= 2001 & mil_conf.df$VET_DATE1 >= 2001 &
                      mil_conf.df$VET_DATE1 < 2080] <- 1 # before and after 9/11
mil_conf.df$VET_911[mil_conf.df$VET_DATE > 2001 & mil_conf.df$VET_DATE < 2080] <- 2 # after 9/11
# VET_911: divides period of service (0 - before, 1 - before/after, 2 - after)

#### GOFO QUESTIONS ####

## Binary variables for correct answers

mil_conf.df$mattis_cor <- NA
mil_conf.df$mattis_cor <- ifelse(mil_conf.df$Q7A == 2, 1, 0)

mil_conf.df$mcmaster_cor <- NA
mil_conf.df$mcmaster_cor <- ifelse(mil_conf.df$Q7B == 2, 1, 0)

mil_conf.df$petraeus_cor <- NA
mil_conf.df$petraeus_cor <- ifelse(mil_conf.df$Q7C == 2, 1, 0)

mil_conf.df$powell_cor <- NA
mil_conf.df$powell_cor <- ifelse(mil_conf.df$Q7D == 2, 1, 0)

mil_conf.df$mcraven_cor <- NA
mil_conf.df$mcraven_cor <- ifelse(mil_conf.df$Q7I == 2, 1, 0)

mil_conf.df$stavridis_cor <- NA
mil_conf.df$stavridis_cor <- ifelse(mil_conf.df$Q7F == 2, 1, 0)

mil_conf.df$goldfein_cor <- NA
mil_conf.df$goldfein_cor <- ifelse(mil_conf.df$Q7E == 1, 1, 0)

mil_conf.df$dunford_cor <- NA
mil_conf.df$dunford_cor <- ifelse(mil_conf.df$Q7G == 1, 1, 0)

mil_conf.df$milley_cor <- NA
mil_conf.df$milley_cor <- ifelse(mil_conf.df$Q7H == 1, 1, 0)

## GOFO knowledge scale (3 point)

mil_conf.df$GOFO <- rowSums(mil_conf.df[, c("mattis_cor", "mcmaster_cor", "petraeus_cor", "powell_cor",
                                            "mcraven_cor", "stavridis_cor", "goldfein_cor", 
                                            "dunford_cor", "milley_cor")], na.rm = TRUE)

## Recode skipped answers to NA

mil_conf.df$mattis_ans <- mil_conf.df$Q7A
mil_conf.df$mattis_ans[mil_conf.df$mattis_ans == 98] <- NA

mil_conf.df$mcmaster_ans <- mil_conf.df$Q7B
mil_conf.df$mcmaster_ans[mil_conf.df$mcmaster_ans == 98] <- NA

mil_conf.df$petraeus_ans <- mil_conf.df$Q7C
mil_conf.df$petraeus_ans[mil_conf.df$petraeus_ans == 98] <- NA

mil_conf.df$powell_ans <- mil_conf.df$Q7D
mil_conf.df$powell_ans[mil_conf.df$powell_ans == 98] <- NA

mil_conf.df$mcraven_ans <- mil_conf.df$Q7I
mil_conf.df$mcraven_ans[mil_conf.df$mcraven_ans == 98] <- NA

mil_conf.df$stavridis_ans <- mil_conf.df$Q7F
mil_conf.df$stavridis_ans[mil_conf.df$stavridis_ans == 98] <- NA

mil_conf.df$goldfein_ans <- mil_conf.df$Q7E
mil_conf.df$goldfein_ans[mil_conf.df$goldfein_ans == 98] <- NA

mil_conf.df$dunford_ans <- mil_conf.df$Q7G
mil_conf.df$dunford_ans[mil_conf.df$dunford_ans == 98] <- NA

mil_conf.df$milley_ans <- mil_conf.df$Q7H
mil_conf.df$milley_ans[mil_conf.df$milley_ans == 98] <- NA

#### FAMILY RESPONSES ####

mil_conf.df$Family <- 0
mil_conf.df$Family[mil_conf.df$Q46A_1 == 1 | mil_conf.df$Q46A_2 == 1 | mil_conf.df$Q46A_3 == 1 |
                     mil_conf.df$Q46A_4 == 1] <- 1

mil_conf.df$Family_2 <- 0
mil_conf.df$Family_2[mil_conf.df$Q46A_1 == 1 | mil_conf.df$Q46A_2 == 1 | mil_conf.df$Q46A_3 == 1 |
                       mil_conf.df$Q46A_4 == 1 | mil_conf.df$Q46A_5] <- 1

#### OTHER CLEANING ####

## Set skipped as missing for Q8, Q9, Q10, Q10A, Q11A

mil_conf.df$Q8_m <- mil_conf.df$Q8 
mil_conf.df$Q8_m[mil_conf.df$Q8 == 98] <- NA
# Q8_m: recodes the skipped (98) as missing (NA)

mil_conf.df$Q8_d <- NA
mil_conf.df$Q8_d <- ifelse(mil_conf.df$Q8_m == 1 | mil_conf.df$Q8_m == 2, 1, 0)
# Q8_d: recodes Q8 into a binary variable

mil_conf.df$Q9_m <- mil_conf.df$Q9_r
mil_conf.df$Q9_m[mil_conf.df$Q9_r == 98] <- NA
# Q9_m: recodes the skipped (98) as missing (NA)

mil_conf.df$Q10_m <- mil_conf.df$Q10
mil_conf.df$Q10_m[mil_conf.df$Q10 == 98] <- NA
# Q10_m: recodes the skipped (98) as missing (NA)

mil_conf.df$Q10A_m <- mil_conf.df$Q10A_r
mil_conf.df$Q10A_m[mil_conf.df$Q10A_r == 98] <- NA
# Q10A_m: recodes the skipped (98) as missing (NA)

mil_conf.df$Q11A_m <- mil_conf.df$Q11A_r
mil_conf.df$Q11A_m[mil_conf.df$Q11A_r == 98] <- NA

## Add control condition to treatment dummy variables

asg_varlist <- c("ASSIGNMENT_A_2", "ASSIGNMENT_A_3", "ASSIGNMENT_A_4", "ASSIGNMENT_A_5",
                 "ASSIGNMENT_A_6", "ASSIGNMENT_A_7", "ASSIGNMENT_A_8")

for (i in asg_varlist) {
  mil_conf.df[[i]][mil_conf.df$ASSIGNMENT_A_1 == 1] <- 0
}

## Create dummy variables to collapse party id

mil_conf.df$rep <- NA
mil_conf.df$rep <- ifelse(mil_conf.df$PARTYID7 >= 5, 1, 0)

mil_conf.df$ind <- NA
mil_conf.df$ind <- ifelse(mil_conf.df$PARTYID7 == 4, 1, 0)

mil_conf.df$dem <- NA
mil_conf.df$dem <- ifelse(mil_conf.df$PARTYID7 <= 3, 1, 0)

mil_conf.df$party <- NA
mil_conf.df$party[mil_conf.df$dem == 1] <- 0
mil_conf.df$party[mil_conf.df$ind == 1] <- 1
mil_conf.df$party[mil_conf.df$rep == 1] <- 2

## Create treatment binary variables for Q41

mil_conf.df$Q41A_a <- ifelse(mil_conf.df$Q41A == 1, 1, 0)
mil_conf.df$Q41B_a <- ifelse(mil_conf.df$Q41B == 1, 1, 0)
mil_conf.df$Q41C_a <- ifelse(mil_conf.df$Q41C == 1, 1, 0)

## Create full dummy variables for Assignment A

mil_conf.df$A_1 <- ifelse(mil_conf.df$DOV_ASSIGNMENT_A == 1, 1, 0)
mil_conf.df$A_2 <- ifelse(mil_conf.df$DOV_ASSIGNMENT_A == 2, 1, 0)
mil_conf.df$A_3 <- ifelse(mil_conf.df$DOV_ASSIGNMENT_A == 3, 1, 0)
mil_conf.df$A_4 <- ifelse(mil_conf.df$DOV_ASSIGNMENT_A == 4, 1, 0)
mil_conf.df$A_5 <- ifelse(mil_conf.df$DOV_ASSIGNMENT_A == 5, 1, 0)
mil_conf.df$A_6 <- ifelse(mil_conf.df$DOV_ASSIGNMENT_A == 6, 1, 0)
mil_conf.df$A_7 <- ifelse(mil_conf.df$DOV_ASSIGNMENT_A == 7, 1, 0)
mil_conf.df$A_8 <- ifelse(mil_conf.df$DOV_ASSIGNMENT_A == 8, 1, 0)

mil_conf.df$Q9_col <- mil_conf.df$Q9_m
mil_conf.df$Q9_col <- recode(mil_conf.df$Q9_col, `1` = 0L, `2` = 0L, `3` = 1L, `4` = 1L, 
                             `5` = 1L, `6` = 2L, `7` = 2L,
                             `8` = 2L, `9` = 3L, `10` = 3L)

#### FACTOR VARS ####

fac_varlist <- c("Q1A", "Q1B", "Q2A", "Q2B_1", "Q2B_2", "Q2B_3", "Q2B_4", "Q2B_5", "Q2B_SKP", "Q3", "Q4",
                 "Q5", "Q6", "Q7A", "Q7B", "Q7C", "Q7D", "Q7E", "Q7F", "Q7G", "Q7H", "Q7I", "Q8", 
                 "Q9", "Q10", "Q10A", "Q11", "Q11A", "Q12A_r2", "Q12B_r2", "Q12C_r2", "Q12D_r2", 
                 "Q12E_r2", "Q12F_r2", "Q9_m", "Q10A_m", "Q11A_m",
                 "Q12G_r2", "Q12H_r2", "Q12I_r2", "Q12J_r2", 
                 "Q13", "Q14", "Q15A", "Q15B", "Q15C", "Q15D", "Q15E",
                 "Q15F", "Q15G", "Q16", "Q16A", "Q16B", "Q17", "Q18", "Q19A_r2", "Q19B_r2", "Q19C_r2", 
                 "Q19D_r2", "Q19E_r2", "Q19F_r2", "Q19G_r2", "Q19H_r2", "Q19AA_r2", "Q19AB_r2", "Q19AC_r2", 
                 "Q19AD_r2", "Q19AE_r2", "Q19AF_r2", "Q31A",
                 "Q19AG_r2", "Q19AH_r2", "Q27A_r1", "Q27B_r1", "Q27C_r1", "Q27D_r1", "Q27E_r1", "Q27F_r1", 
                 "Q27G_r1", "Q27H_r1", "Q27I_r1", "Q27J_r1", "Q28AA", "Q28AB", "Q28AC", "Q28AD",
                 "Q31", "Q28A", "Q28B", "Q28C", "Q28D", "party",
                 "Q33A_r2", "Q33B_r2", "Q33C_r2", "Q33D_r2", "Q33E_r2", "Q33F_r2", "Q33G_r2", "Q33H_r2",
                 "Q33AA_r2", "Q33AB_r2", "Q33AC_r2", "Q33AD_r2", "Q33AE_r2", "Q33AF_r2",
                 "Q33AG_r2", "Q33AH_r2", "Q42A_r2", "Q42B_r2", "Q42C_r2", "Q42D_r2", "Q42E_r2", "Q42F_r2", "Q42G_r2",
                 "AGE7", "GENDER", "RACETHNICITY", "EDUC", "VETERAN2", "VETERAN", "Family_2", "Q45",
                 "URBAN3", "Q44", "Q46", "Q47", "Q41A_a", "Q41B_a", "Q41C_a", "Q41D", "Q41E")

for (i in fac_varlist) {
  mil_conf.df[[paste0(i, "_fac")]] <- factor(mil_conf.df[[i]])
}

mil_conf.df$Q27A_fac <- factor(mil_conf.df$Q27A_r1)
mil_conf.df$Q27B_fac <- factor(mil_conf.df$Q27B_r1)
mil_conf.df$Q27C_fac <- factor(mil_conf.df$Q27C_r1)
mil_conf.df$Q27D_fac <- factor(mil_conf.df$Q27D_r1)
mil_conf.df$Q27E_fac <- factor(mil_conf.df$Q27E_r1)
mil_conf.df$Q27F_fac <- factor(mil_conf.df$Q27F_r1)
mil_conf.df$Q27G_fac <- factor(mil_conf.df$Q27G_r1)
mil_conf.df$Q27H_fac <- factor(mil_conf.df$Q27H_r1)
mil_conf.df$Q27I_fac <- factor(mil_conf.df$Q27I_r1)
mil_conf.df$Q27J_fac <- factor(mil_conf.df$Q27J_r1)
mil_conf.df$party_fac <- factor(mil_conf.df$party)

#### CLEANING FOR CH.2 DEMOGRAPHICS ####

## Q8 (also Q8_m, Q8_d)
mil_conf.df$Q8_mr <- car::recode(mil_conf.df$Q8_m, "1=1;2=0.67;3=0.33;4=0")

## Religion
mil_conf.df$catholic <- ifelse(mil_conf.df$RELIG == 2, 1, 0)
mil_conf.df$christian <- ifelse(mil_conf.df$RELIG == 1 | mil_conf.df$RELIG == 3 |
                                  mil_conf.df$RELIG == 4 | mil_conf.df$RELIG == 12, 1, 0)
mil_conf.df$norelig <- ifelse(mil_conf.df$RELIG >= 9 & mil_conf.df$RELIG <= 11, 1, 0)
mil_conf.df$religion <- ifelse(mil_conf.df$christian == 1, 1, 0)
mil_conf.df$religion[mil_conf.df$catholic == 1] <- 2
mil_conf.df$religion[mil_conf.df$RELIG >= 5 & mil_conf.df$RELIG <=8 | 
                       mil_conf.df$RELIG == 13 | mil_conf.df$RELIG == 14] <- 3
mil_conf.df$religion[mil_conf.df$norelig == 1] <- 4

## Ideology
mil_conf.df$ideo_m <- ifelse(mil_conf.df$IDEO == 8, NA, mil_conf.df$IDEO) # DK as NA
mil_conf.df$ideo_r <- ifelse(mil_conf.df$IDEO == 8, 4, mil_conf.df$IDEO) # DK as moderate
mil_conf.df$ideo3 <- NA
mil_conf.df$ideo3[mil_conf.df$ideo_m <= 3] <- 0
mil_conf.df$ideo3[mil_conf.df$ideo_m == 4] <- 1
mil_conf.df$ideo3[mil_conf.df$ideo_m >= 5] <- 2

## Gender
mil_conf.df$male <- ifelse(mil_conf.df$GENDER == 1, 1, 0)

## Race
mil_conf.df$black <- ifelse(mil_conf.df$RACETHNICITY == 2, 1, 0)
mil_conf.df$hispanic <- ifelse(mil_conf.df$RACETHNICITY == 4, 1, 0)
mil_conf.df$other_race <- ifelse(mil_conf.df$RACETHNICITY == 3 | mil_conf.df$RACETHNICITY == 5 |
                                   mil_conf.df$RACETHNICITY == 6, 1, 0)

## Income
mil_conf.df$income5 <- NA
mil_conf.df$income5[mil_conf.df$INCOME <= 5] <- 1
mil_conf.df$income5[mil_conf.df$INCOME >= 6 & mil_conf.df$INCOME >= 9] <- 2
mil_conf.df$income5[mil_conf.df$INCOME >= 10 & mil_conf.df$INCOME >= 12] <- 3
mil_conf.df$income5[mil_conf.df$INCOME >= 13 & mil_conf.df$INCOME >= 15] <- 4
mil_conf.df$income5[mil_conf.df$INCOME >= 16] <- 5

## Generation
mil_conf.df$boomer <- ifelse(mil_conf.df$AGE >= 55 & mil_conf.df$AGE <= 73, 1, 0)
mil_conf.df$genx <- ifelse(mil_conf.df$AGE >= 39 & mil_conf.df$AGE <= 54, 1, 0)
mil_conf.df$millen <- ifelse(mil_conf.df$AGE >= 23 & mil_conf.df$AGE <= 38, 1, 0)
mil_conf.df$generation <- NA
mil_conf.df$generation[mil_conf.df$AGE >= 74 & mil_conf.df$AGE <= 100] <- 1 # silent
mil_conf.df$generation[mil_conf.df$AGE >= 55 & mil_conf.df$AGE <= 73] <- 2 # boomer
mil_conf.df$generation[mil_conf.df$AGE >= 39 & mil_conf.df$AGE <= 54] <- 3 # gen x
mil_conf.df$generation[mil_conf.df$AGE >= 23 & mil_conf.df$AGE <= 38] <- 4 # millennial
mil_conf.df$generation[mil_conf.df$AGE <= 22] <- 5 # gen z

## Veteran Status
mil_conf.df$vet <- ifelse(mil_conf.df$VETERAN == 1 & mil_conf.df$VETERAN2 == 2, 1, 0)
mil_conf.df$activeduty <- ifelse(mil_conf.df$VETERAN == 1 & mil_conf.df$VETERAN2 == 1, 1, 0)
mil_conf.df$vetstatus <- NA
mil_conf.df$vetstatus[mil_conf.df$VETERAN == 1 & mil_conf.df$VETERAN2 == 1] <- 1
mil_conf.df$vetstatus[mil_conf.df$VETERAN == 1 & mil_conf.df$VETERAN2 == 2] <- 2
mil_conf.df$vetstatus[mil_conf.df$VETERAN == 2] <- 3

mil_conf.df$social <- ifelse(mil_conf.df$Q45 == 1, 1, 0)
mil_conf.df$family <- ifelse(mil_conf.df$Q46 == 1, 1, 0)

## Region
mil_conf.df$midwest <- ifelse(mil_conf.df$REGION4 == 2, 1, 0)
mil_conf.df$south <- ifelse(mil_conf.df$REGION4 == 3, 1, 0)
mil_conf.df$west <- ifelse(mil_conf.df$REGION4 == 4, 1, 0)

mil_conf.df$city <- ifelse(mil_conf.df$URBAN3 == 1, 1, 0)
mil_conf.df$rural <- ifelse(mil_conf.df$URBAN3 == 3, 1, 0)

## Employment
mil_conf.df$unemployed <- ifelse(mil_conf.df$EMPLOY <= 2 | mil_conf.df$EMPLOY == 5, 0, 1)

## Education 
mil_conf.df$edulvl <- NA
mil_conf.df$edulvl[mil_conf.df$EDUC <= 9] <- 1
mil_conf.df$edulvl[mil_conf.df$EDUC >= 10 & mil_conf.df$EDUC <= 11] <- 2
mil_conf.df$edulvl[mil_conf.df$EDUC == 12] <- 3
mil_conf.df$edulvl[mil_conf.df$EDUC >= 13] <- 4

## Marital Status
mil_conf.df$married <- ifelse(mil_conf.df$MARITAL == 1, 1, 0)

#### CLEANING FOR CH.6 ####

## Military attributes (Q12A-G)
mil_conf.df$Q12A_d <- ifelse(mil_conf.df$Q12A == 1 | mil_conf.df$Q12A == 2, 1, 0)
mil_conf.df$Q12A_d[mil_conf.df$Q12A == 98] <- NA
mil_conf.df$Q12B_d <- ifelse(mil_conf.df$Q12B == 1 | mil_conf.df$Q12B == 2, 1, 0)
mil_conf.df$Q12B_d[mil_conf.df$Q12B == 98] <- NA
mil_conf.df$Q12C_d <- ifelse(mil_conf.df$Q12C == 1 | mil_conf.df$Q12C == 2, 1, 0)
mil_conf.df$Q12C_d[mil_conf.df$Q12C == 98] <- NA
mil_conf.df$Q12D_d <- ifelse(mil_conf.df$Q12D == 1 | mil_conf.df$Q12D == 2, 1, 0)
mil_conf.df$Q12D_d[mil_conf.df$Q12D == 98] <- NA
mil_conf.df$Q12E_d <- ifelse(mil_conf.df$Q12E == 1 | mil_conf.df$Q12E == 2, 1, 0)
mil_conf.df$Q12E_d[mil_conf.df$Q12E == 98] <- NA
mil_conf.df$Q12F_d <- ifelse(mil_conf.df$Q12F == 1 | mil_conf.df$Q12F == 2, 1, 0)
mil_conf.df$Q12F_d[mil_conf.df$Q12F == 98] <- NA
mil_conf.df$Q12H_d <- ifelse(mil_conf.df$Q12H == 1 | mil_conf.df$Q12H == 2, 1, 0)
mil_conf.df$Q12H_d[mil_conf.df$Q12H == 98] <- NA

## Other institution attributes (A15A-G)
mil_conf.df$Q15A_d <- ifelse(mil_conf.df$Q15A == 1 | mil_conf.df$Q15A == 2, 1, 0)
mil_conf.df$Q15A_d[mil_conf.df$Q15A == 98] <- NA
mil_conf.df$Q15B_d <- ifelse(mil_conf.df$Q15B == 1 | mil_conf.df$Q15B == 2, 1, 0)
mil_conf.df$Q15B_d[mil_conf.df$Q15B == 98] <- NA
mil_conf.df$Q15C_d <- ifelse(mil_conf.df$Q15C == 1 | mil_conf.df$Q15C == 2, 1, 0)
mil_conf.df$Q15C_d[mil_conf.df$Q15C == 98] <- NA
mil_conf.df$Q15D_d <- ifelse(mil_conf.df$Q15D == 1 | mil_conf.df$Q15D == 2, 1, 0)
mil_conf.df$Q15D_d[mil_conf.df$Q15D == 98] <- NA
mil_conf.df$Q15E_d <- ifelse(mil_conf.df$Q15E == 1 | mil_conf.df$Q15E == 2, 1, 0)
mil_conf.df$Q15E_d[mil_conf.df$Q15E == 98] <- NA
mil_conf.df$Q15F_d <- ifelse(mil_conf.df$Q15F == 1 | mil_conf.df$Q15F == 2, 1, 0)
mil_conf.df$Q15F_d[mil_conf.df$Q15F == 98] <- NA
mil_conf.df$Q15G_d <- ifelse(mil_conf.df$Q15G == 1 | mil_conf.df$Q15G == 2, 1, 0)
mil_conf.df$Q15G_d[mil_conf.df$Q15G == 98] <- NA

#### TREATMENT SUBSETS - A2-4, PARTY ####

mil_conf.df$A2_rep <- NA
mil_conf.df$A2_rep[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 2] <- 0
mil_conf.df$A2_rep[mil_conf.df$DOV_ASSIGNMENT_A == 2 & mil_conf.df$party == 2] <- 1

mil_conf.df$A2_ind <- NA
mil_conf.df$A2_ind[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 1] <- 0
mil_conf.df$A2_ind[mil_conf.df$DOV_ASSIGNMENT_A == 2 & mil_conf.df$party == 1] <- 1

mil_conf.df$A2_dem <- NA
mil_conf.df$A2_dem[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 0] <- 0
mil_conf.df$A2_dem[mil_conf.df$DOV_ASSIGNMENT_A == 2 & mil_conf.df$party == 0] <- 1

mil_conf.df$A3_rep <- NA
mil_conf.df$A3_rep[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 2] <- 0
mil_conf.df$A3_rep[mil_conf.df$DOV_ASSIGNMENT_A == 3 & mil_conf.df$party == 2] <- 1

mil_conf.df$A3_ind <- NA
mil_conf.df$A3_ind[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 1] <- 0
mil_conf.df$A3_ind[mil_conf.df$DOV_ASSIGNMENT_A == 3 & mil_conf.df$party == 1] <- 1

mil_conf.df$A3_dem <- NA
mil_conf.df$A3_dem[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 0] <- 0
mil_conf.df$A3_dem[mil_conf.df$DOV_ASSIGNMENT_A == 3 & mil_conf.df$party == 0] <- 1

mil_conf.df$A4_rep <- NA
mil_conf.df$A4_rep[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 2] <- 0
mil_conf.df$A4_rep[mil_conf.df$DOV_ASSIGNMENT_A == 4 & mil_conf.df$party == 2] <- 1

mil_conf.df$A4_ind <- NA
mil_conf.df$A4_ind[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 1] <- 0
mil_conf.df$A4_ind[mil_conf.df$DOV_ASSIGNMENT_A == 4 & mil_conf.df$party == 1] <- 1

mil_conf.df$A4_dem <- NA
mil_conf.df$A4_dem[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 0] <- 0
mil_conf.df$A4_dem[mil_conf.df$DOV_ASSIGNMENT_A == 4 & mil_conf.df$party == 0] <- 1

#### TREATMENT SUBSETS - A5-6, 7-8, PARTY ####

mil_conf.df$A5_rep <- NA
mil_conf.df$A5_rep[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 2] <- 0
mil_conf.df$A5_rep[mil_conf.df$DOV_ASSIGNMENT_A == 5 & mil_conf.df$party == 2] <- 1

mil_conf.df$A5_ind <- NA
mil_conf.df$A5_ind[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 1] <- 0
mil_conf.df$A5_ind[mil_conf.df$DOV_ASSIGNMENT_A == 5 & mil_conf.df$party == 1] <- 1

mil_conf.df$A5_dem <- NA
mil_conf.df$A5_dem[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 0] <- 0
mil_conf.df$A5_dem[mil_conf.df$DOV_ASSIGNMENT_A == 5 & mil_conf.df$party == 0] <- 1

mil_conf.df$A6_rep <- NA
mil_conf.df$A6_rep[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 2] <- 0
mil_conf.df$A6_rep[mil_conf.df$DOV_ASSIGNMENT_A == 6 & mil_conf.df$party == 2] <- 1

mil_conf.df$A6_ind <- NA
mil_conf.df$A6_ind[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 1] <- 0
mil_conf.df$A6_ind[mil_conf.df$DOV_ASSIGNMENT_A == 6 & mil_conf.df$party == 1] <- 1

mil_conf.df$A6_dem <- NA
mil_conf.df$A6_dem[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 0] <- 0
mil_conf.df$A6_dem[mil_conf.df$DOV_ASSIGNMENT_A == 6 & mil_conf.df$party == 0] <- 1

mil_conf.df$A7_rep <- NA
mil_conf.df$A7_rep[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 2] <- 0
mil_conf.df$A7_rep[mil_conf.df$DOV_ASSIGNMENT_A == 7 & mil_conf.df$party == 2] <- 1

mil_conf.df$A7_ind <- NA
mil_conf.df$A7_ind[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 1] <- 0
mil_conf.df$A7_ind[mil_conf.df$DOV_ASSIGNMENT_A == 7 & mil_conf.df$party == 1] <- 1

mil_conf.df$A7_dem <- NA
mil_conf.df$A7_dem[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 0] <- 0
mil_conf.df$A7_dem[mil_conf.df$DOV_ASSIGNMENT_A == 7 & mil_conf.df$party == 0] <- 1

mil_conf.df$A8_rep <- NA
mil_conf.df$A8_rep[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 2] <- 0
mil_conf.df$A8_rep[mil_conf.df$DOV_ASSIGNMENT_A == 8 & mil_conf.df$party == 2] <- 1

mil_conf.df$A8_ind <- NA
mil_conf.df$A8_ind[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 1] <- 0
mil_conf.df$A8_ind[mil_conf.df$DOV_ASSIGNMENT_A == 8 & mil_conf.df$party == 1] <- 1

mil_conf.df$A8_dem <- NA
mil_conf.df$A8_dem[mil_conf.df$DOV_ASSIGNMENT_A == 1 & mil_conf.df$party == 0] <- 0
mil_conf.df$A8_dem[mil_conf.df$DOV_ASSIGNMENT_A == 8 & mil_conf.df$party == 0] <- 1

#### TREATMENT SUBSETS - C3-6, PARTY ####

mil_conf.df$C3_rep <- NA
mil_conf.df$C3_rep[mil_conf.df$DOV_ASSIGNMENT_C == 2 & mil_conf.df$party == 2] <- 0
mil_conf.df$C3_rep[mil_conf.df$DOV_ASSIGNMENT_C == 3 & mil_conf.df$party == 2] <- 1

mil_conf.df$C4_rep <- NA
mil_conf.df$C4_rep[mil_conf.df$DOV_ASSIGNMENT_C == 2 & mil_conf.df$party == 2] <- 0
mil_conf.df$C4_rep[mil_conf.df$DOV_ASSIGNMENT_C == 4 & mil_conf.df$party == 2] <- 1

mil_conf.df$C5_rep <- NA
mil_conf.df$C5_rep[mil_conf.df$DOV_ASSIGNMENT_C == 2 & mil_conf.df$party == 2] <- 0
mil_conf.df$C5_rep[mil_conf.df$DOV_ASSIGNMENT_C == 5 & mil_conf.df$party == 2] <- 1

mil_conf.df$C6_rep <- NA
mil_conf.df$C6_rep[mil_conf.df$DOV_ASSIGNMENT_C == 2 & mil_conf.df$party == 2] <- 0
mil_conf.df$C6_rep[mil_conf.df$DOV_ASSIGNMENT_C == 6 & mil_conf.df$party == 2] <- 1

mil_conf.df$C3_ind <- NA
mil_conf.df$C3_ind[mil_conf.df$DOV_ASSIGNMENT_C == 2 & mil_conf.df$party == 1] <- 0
mil_conf.df$C3_ind[mil_conf.df$DOV_ASSIGNMENT_C == 3 & mil_conf.df$party == 1] <- 1

mil_conf.df$C4_ind <- NA
mil_conf.df$C4_ind[mil_conf.df$DOV_ASSIGNMENT_C == 2 & mil_conf.df$party == 1] <- 0
mil_conf.df$C4_ind[mil_conf.df$DOV_ASSIGNMENT_C == 4 & mil_conf.df$party == 1] <- 1

mil_conf.df$C5_ind <- NA
mil_conf.df$C5_ind[mil_conf.df$DOV_ASSIGNMENT_C == 2 & mil_conf.df$party == 1] <- 0
mil_conf.df$C5_ind[mil_conf.df$DOV_ASSIGNMENT_C == 5 & mil_conf.df$party == 1] <- 1

mil_conf.df$C6_ind <- NA
mil_conf.df$C6_ind[mil_conf.df$DOV_ASSIGNMENT_C == 2 & mil_conf.df$party == 1] <- 0
mil_conf.df$C6_ind[mil_conf.df$DOV_ASSIGNMENT_C == 6 & mil_conf.df$party == 1] <- 1

mil_conf.df$C3_dem <- NA
mil_conf.df$C3_dem[mil_conf.df$DOV_ASSIGNMENT_C == 2 & mil_conf.df$party == 0] <- 0
mil_conf.df$C3_dem[mil_conf.df$DOV_ASSIGNMENT_C == 3 & mil_conf.df$party == 0] <- 1

mil_conf.df$C4_dem <- NA
mil_conf.df$C4_dem[mil_conf.df$DOV_ASSIGNMENT_C == 2 & mil_conf.df$party == 0] <- 0
mil_conf.df$C4_dem[mil_conf.df$DOV_ASSIGNMENT_C == 4 & mil_conf.df$party == 0] <- 1

mil_conf.df$C5_dem <- NA
mil_conf.df$C5_dem[mil_conf.df$DOV_ASSIGNMENT_C == 2 & mil_conf.df$party == 0] <- 0
mil_conf.df$C5_dem[mil_conf.df$DOV_ASSIGNMENT_C == 5 & mil_conf.df$party == 0] <- 1

mil_conf.df$C6_dem <- NA
mil_conf.df$C6_dem[mil_conf.df$DOV_ASSIGNMENT_C == 2 & mil_conf.df$party == 0] <- 0
mil_conf.df$C6_dem[mil_conf.df$DOV_ASSIGNMENT_C == 6 & mil_conf.df$party == 0] <- 1

#### NEGATIVE PARTISANSHIP ####

mil_conf.df$A_copartisan <- NA
mil_conf.df$A_copartisan[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0 # control
mil_conf.df$A_copartisan[mil_conf.df$DOV_ASSIGNMENT_A == 2 & mil_conf.df$party == 2] <- 1 # co-partisan
mil_conf.df$A_copartisan[mil_conf.df$DOV_ASSIGNMENT_A == 3 & mil_conf.df$party == 0] <- 1
mil_conf.df$A_copartisan[mil_conf.df$DOV_ASSIGNMENT_A == 2 & mil_conf.df$party == 0] <- 2 # cross-partisan
mil_conf.df$A_copartisan[mil_conf.df$DOV_ASSIGNMENT_A == 3 & mil_conf.df$party == 2] <- 2
mil_conf.df$A_copartisan[mil_conf.df$party == 1] <- NA

mil_conf.df$A_copartisanc <- NA
mil_conf.df$A_copartisanc[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0 # control
mil_conf.df$A_copartisanc[mil_conf.df$DOV_ASSIGNMENT_A == 2 & mil_conf.df$party == 2] <- 1 # co-partisan
mil_conf.df$A_copartisanc[mil_conf.df$DOV_ASSIGNMENT_A == 3 & mil_conf.df$party == 0] <- 1
mil_conf.df$A_copartisanc[mil_conf.df$party == 1] <- NA

mil_conf.df$A_copartisanx <- NA
mil_conf.df$A_copartisanx[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0 # control
mil_conf.df$A_copartisanx[mil_conf.df$DOV_ASSIGNMENT_A == 2 & mil_conf.df$party == 0] <- 1 # cross-partisan
mil_conf.df$A_copartisanx[mil_conf.df$DOV_ASSIGNMENT_A == 3 & mil_conf.df$party == 2] <- 1
mil_conf.df$A_copartisanx[mil_conf.df$party == 1] <- NA

#### SAVE CLEAN DATA ####
save(mil_conf.df, file = "~/Dropbox/Public_Conf_Mil/Data_and_code/wave1_data/clean_dataw1.RData")
